﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SW2A311_Lib
{
    /// <summary>
    /// Task-strukturen, der laver opgave elementer til de enkelte projekter.
    /// </summary>
    [Serializable]  //  Attribut; Objektet sættes til at være serialisérbart. Skal det være da task objekter genereres i projectDB der er serialisérbar.
    public class Task : ISerializable
    {
        public DateTime TaskEnd
        {
            get { return _TaskEnd; }
            set
            {
                if (value >= TaskStart)
                    _TaskEnd = value;
                else
                    throw new ArgumentOutOfRangeException("TaskEnd", "Must be nonzero and equal to or after: " + TaskStart.ToShortDateString());
            }
        }
        private DateTime _TaskEnd;

        public DateTime TaskStart
        {
            get { return _TaskStart; }
            set
            {
                if (value != DateTime.MinValue)
                    _TaskStart = value;
                else
                    throw new ArgumentOutOfRangeException("TaskStart", "Must be nonzero");
            }

        }
        private DateTime _TaskStart;

        public string TaskName
        {
            get { return _TaskName; }
            set
            {
                if (string.IsNullOrWhiteSpace(value))                   // Tjekker om task har et navn.
                {
                    throw new ArgumentNullException("Task does not have a valid name!");
                }
                else { _TaskName = value; }
            }
        }
        private string _TaskName;

        public List<string> AssignedMembers { get; set; }
        public List<string> Dependancies { get; set; }
        public string TaskDescription { get; set; }
        /// <summary>
        /// Standard construktor til Task objekter.
        /// </summary>
        /// <param name="taskEnd"></param>
        /// <param name="taskStart"></param>
        /// <param name="taskName"></param>
        /// <param name="taskDescription"></param>
        public Task(DateTime taskEnd, DateTime taskStart, string taskName, string taskDescription)
        {
            TaskStart = taskStart;
            TaskEnd = taskEnd;
            TaskName = taskName;
            TaskDescription = taskDescription;
            AssignedMembers = new List<string>();
            Dependancies = new List<string>();
        }
        /// <summary>
        /// Constructer til når objektet skal serialiseres.
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public Task(SerializationInfo info, StreamingContext ctxt)
        {
            // Det er kun de attributes der nævnes her der bliver serialiseret
            TaskEnd = (DateTime)info.GetValue("TaskEnd", typeof(DateTime));
            TaskStart = (DateTime)info.GetValue("TaskStart", typeof(DateTime));
            AssignedMembers = (List<String>)info.GetValue("AssignedMembers", typeof(List<string>));
            Dependancies = (List<String>)info.GetValue("Dependancies", typeof(List<string>));
            TaskName = (String)info.GetValue("TaskName", typeof(string));
            TaskDescription = (String)info.GetValue("TaskDescription", typeof(string));
        }
        /// <summary>
        /// Metode der bruges til at deserialisere objektet.
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public virtual void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            /* Skal indeholde de samme attributes som serialiserings-Constructeren
             * og de enkelte strings skal matche dem fra constructeren.
             */
            info.AddValue("TaskEnd", this.TaskEnd);
            info.AddValue("TaskStart", this.TaskStart);
            info.AddValue("AssignedMembers", this.AssignedMembers);
            info.AddValue("Dependancies", this.Dependancies);
            info.AddValue("TaskName", this.TaskName);
            info.AddValue("TaskDescription", this.TaskDescription);
        }

        /// <summary>
        /// Override af ToString metoden
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string memberString = "No members added.";
            if (AssignedMembers.Count > 0)
            {
                memberString = string.Join(", ", AssignedMembers);
            }
            return string.Format("Task Name: {0} Task Start: {1} Task End: {2} \nMembers: {3}", TaskName, TaskStart, TaskEnd, memberString);
        }
    }
}
